// SpeeedReaderSetup.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "ce_setup.h"
#include "SpeeedReaderSetup.h"

BOOL _remove = FALSE;
LPTSTR _program_path = L"\\Program Files\\Speeed Reader";
LPTSTR _cache_path = L"\\Program Files\\Speeed Reader\\SpeeedReaderCache";

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

codeINSTALL_INIT
Install_Init(
    HWND        hwndParent,
    BOOL        fFirstCall,     // is this the first time this function is being called?
    BOOL        fPreviouslyInstalled,
    LPCTSTR     pszInstallDir
)
{
	//InitializePaths();

	// To continue installation, return codeINSTALL_INIT_CONTINUE
	// If you want to cancel installation, 
	// return codeINSTALL_EXIT_UNINSTALL
	return codeINSTALL_INIT_CONTINUE;

}

codeINSTALL_EXIT
Install_Exit(
    HWND    hwndParent,
    LPCTSTR pszInstallDir,
    WORD    cFailedDirs,
    WORD    cFailedFiles,
    WORD    cFailedRegKeys,
    WORD    cFailedRegVals,
    WORD    cFailedShortcuts
)
{
	WIN32_FIND_DATA findfiledata;
	HANDLE hfind;
	hfind = FindFirstFile(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak", &findfiledata);
	if(hfind != INVALID_HANDLE_VALUE)
	{
		DeleteFile(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini");
		CopyFile(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak", 
			L"\\Program Files\\Speeed Reader\\SpeeedReader.ini", false);
		DeleteFile(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak");
	}
	/*
	if(::MessageBoxW(hwndParent, L"Do you want to start Speeed Reader now?", L"Speeed Reader", MB_YESNO) == IDYES)
	{
		PROCESS_INFORMATION pi;
		CreateProcess(NULL, L"\\Program Files\\Speeed Reader\\SpeeedReader.exe", NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
	}*/		

	// To exit the installation DLL normally, 
	// return codeINSTALL_EXIT_DONE
	// To unistall the application after the function exits,
	// return codeINSTALL_EXIT_UNINSTALL
	return codeINSTALL_EXIT_DONE;
}

codeUNINSTALL_INIT
Uninstall_Init(HWND hwndParent, LPCTSTR pszInstallDir)
{
	//local variables    

    WIN32_FIND_DATA findfiledata;
    HANDLE hfind;
    TCHAR pszfilepath[100];
	TCHAR pszconfigfilepath[100];

    //initialize character array
    memset(pszfilepath,0,sizeof(pszfilepath));

    //copy database path to character array
    //pszinstalldir variable will contain the 
    //application path (eg : \Program Files\TestApp)
    wcscpy(pszfilepath,pszInstallDir);
    wcscat(pszfilepath,TEXT("\\SR_Error_Log.txt"));

	wcscpy(pszconfigfilepath,pszInstallDir);
    wcscat(pszconfigfilepath,TEXT("\\SpeeedReader.ini"));

	CopyFile(pszconfigfilepath, L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak", false);

    //trying to find whether database file exists or not
    hfind = FindFirstFile(pszfilepath,&findfiledata);
    if(hfind != INVALID_HANDLE_VALUE) //database file exists
    {
        //delete database
        DeleteFile(pszfilepath);
    }
	if(::MessageBoxW(NULL, L"Do you want to keep your user settings & article cache?", L"Speeed Reader", MB_YESNO) == IDNO)
	{
		DeleteFile(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak");
		DestroyDirectory(_cache_path);
		DestroyDirectory(L"\\Storage Card\\SpeeedReaderCache");
		_remove = TRUE;
	}	

	// To continue uninstallation, return codeUNINSTALL_INIT_CONTINUE
	// If you want to cancel installation,
	// return codeUNINSTALL_INIT_CANCEL
	return codeUNINSTALL_INIT_CONTINUE;
}

codeUNINSTALL_EXIT
Uninstall_Exit(HWND hwndParent)
{
	// TODO: Add custom uninstallation code here
	BOOL exists = DirectoryExist(L"\\Program Files\\Speeed Reader\\SpeeedReader.ini.bak");
	if(!exists)
	{
		DestroyDirectory(_program_path);
	}
	return codeUNINSTALL_EXIT_DONE;
}

BOOL DirectoryExist(LPTSTR tszDir)
{
	TCHAR tszString[MAX_PATH * 2];  
	HANDLE hFind = INVALID_HANDLE_VALUE;  	  
	WIN32_FIND_DATA FindFileData;  
	  
	if(!tszDir)
	{
		return FALSE;  
	} 	  
	wcscpy(tszString,tszDir);
  
	hFind = FindFirstFile(tszString, &FindFileData);  	  
	if(hFind == INVALID_HANDLE_VALUE)  	  
	{  	  
		// error	  
		return FALSE;			  
	}  

	return TRUE;
}

BOOL InitializePaths()
{
	TCHAR pszfilepath[100];
    //initialize character array
    memset(pszfilepath,0,sizeof(pszfilepath));

	LPTSTR program_files = NULL;
	SHGetSpecialFolderPath(NULL, program_files, CSIDL_PROGRAM_FILES, false);
	::MessageBox(NULL, program_files, L"test", MB_YESNO);
	/*
	wcscpy(pszfilepath, L"\\");
	wcscat(pszfilepath, program_files);
	wcscat(pszfilepath, L"\\Speeed Reader");
	_program_path = pszfilepath;

	wcscat(pszfilepath, L"\\SpeeedReaderCache");
	_cache_path = pszfilepath;*/

	return TRUE;
}

BOOL DestroyDirectory(LPTSTR tszDir)   
{    
	TCHAR tszString[MAX_PATH * 2];  
	HANDLE hFind = INVALID_HANDLE_VALUE;  	  
	WIN32_FIND_DATA FindFileData;  
	  
	if(!tszDir)
	{
		return FALSE;  
	}
	  
	_stprintf(tszString, TEXT("%s\\*"), tszDir);  	  
	hFind = FindFirstFile(tszString, &FindFileData);  	  
	if(hFind == INVALID_HANDLE_VALUE)  	  
	{  	  
		// error	  
		goto exitFunction;			  
	}  
	  
	do 
	{	  
		_stprintf(tszString, TEXT("%s\\%s"), tszDir, FindFileData.cFileName);  		  
		if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  	  
		{  	  
			if(_tcsicmp(FindFileData.cFileName, _T(".")) == 0)  	  
				continue; 	  
			if(_tcsicmp(FindFileData.cFileName, _T("..")) == 0)  
				continue;	  
			if(!DestroyDirectory(tszString))  	  
			 goto exitFunction; 	  
		}  
	  
		else 
		{  			  
			if(!DeleteFile(tszString))  			  
				goto exitFunction;				  
		}  
	  
	} while(FindNextFile(hFind, &FindFileData));  
	  
	exitFunction:	  
	if(hFind != INVALID_HANDLE_VALUE)  	  
		FindClose(hFind);  
	  
	return RemoveDirectory(tszDir);   
}